// pages/card/detail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    swiperList:[],
    id:0,
    orderId:0,
    showPayBox: false,
    info:{id:0}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({
      id: options.id
    })
    this.load();
  },
  onShow() {
    if(this.data.info.id>0){
      this.load();
    }
  },
  async load(){
    let res=await wx.util.post("card/detail",{id:this.data.id});
    wx.stopPullDownRefresh();
    this.setData({
      info: res.data,
      swiperList: res.data.img_images_arr,
    })
  },
  async buyNow(){
    try{
      let res=await wx.util.post("card_order/create",{id:this.data.id},false);
      this.setData({
        orderId:res.data.orderId,
        showPayBox:true
      })
    }catch(e){
      console.log(e)
      if(e.code==100){
        wx.navigateTo({
          url:'/pages/login/login'
        })
      }
    }
  },
  goHome(){
    wx.switchTab({
      url:"/pages/index/index"
    })
  },
  previewImage(e) {
    let index = e.currentTarget.dataset.index;
    wx.previewImage({
      current: this.data.swiperList[index],
      urls: this.data.swiperList
    });
  },
  closePayBox(e) {
    console.log(e.detail)
    this.setData({
      showPayBox:false
    })
    if(e.detail.url){
      wx.redirectTo({
        url:e.detail.url
      })
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.load();
  },

  onShareTimeline: function (e) {
    let wxid=wx.getStorageSync("openid") || '';
    return {
      query: 'id='+this.data.id+'&wxid='+wxid,
    }
  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    let wxid=wx.getStorageSync("openid") || '';
    return {
      title: this.data.info.name,
      path: '/pages/card/detail?id='+this.data.id+'wxid='+wxid,
    }
  }
})